﻿using Lamex.Base;
using Lamex.Interface;
using Lamex.Library;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mCost.UI
{
    public partial class frmCOST_MOTP_Transfer : frmBaseChildWinForm
    {
        /* 1、窗体尺寸需跟父窗体一致，不然会导致控件尺寸异常 
           2、sapnco.dll和sapnco_utils.dll 必须是64位并且支持.Net.4.5.2，不然没法与sap系统交互
        */
        public frmCOST_MOTP_Transfer(string dtName, int iType, bool isEdit, bool isRefresh)
            : base(dtName, iType, isEdit, isRefresh)
        {
            InitializeComponent();
        }
        public override void DoImport(IButtonInfo sender)
        {
            using (DevExpress.Utils.WaitDialogForm wdf = new DevExpress.Utils.WaitDialogForm("请稍等", "正在从SAP下载数据......", new Size(400, 80)))
            {
                string strError = "";
                Common.SAPHelper sAPHelper = new Common.SAPHelper();
                //从sap获取数据
                DataTable dataTable = new DataTable();
                dataTable = sAPHelper.GetSAPDataTable(Common.GetDataTable.GetTransfer_Package_List(),
                    "I_TAB",
                    Common.GetTable.GetSAP_TABLE(),
                    "ZFM_MES_GET_TP",
                    Common.GetTable.GetSAP_LIST()
                    ,"LXP"
                    , out strError);
                if (dataTable.Rows.Count<=0)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("无法取得SAP数据,导入失败！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                //添加导入时间和导入用户
                DataColumn dc1 = new DataColumn("UPDATE_TIME", typeof(DateTime));
                dc1.DefaultValue = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                dataTable.Columns.Add(dc1);
                DataColumn dc2 = new DataColumn("UPDATE_USER", typeof(string));
                dc2.DefaultValue = Lamex.Common.Loginer.CurrentUser.AccountName;
                dataTable.Columns.Add(dc2);
                //插入数据库
                if (BaseBuildSql.ExcuteInsert(dataTable, "Transfer_Package_MO_TP"))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("数据导入成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }
        }
    }
}
